package com.iyundao.io.Service.Impl;

import com.iyundao.io.Service.StatisticsService;
import com.iyundao.io.mapper.PartyMemberCustomMapper;
import com.iyundao.io.mapper.PartyMemberMapper;
import com.iyundao.io.mapper.StatisticsMapper;
import com.iyundao.io.mapper.SysDeptCustomMapper;
import com.iyundao.io.model.*;
import com.iyundao.io.tools.PartymemberVo;
import com.iyundao.io.tools.ResponseData;
import com.iyundao.io.tools.StaticsShow;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StatisticsServiceImpl implements StatisticsService {


    @Autowired
    private PartyMemberCustomMapper memberCustomMapper;

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Autowired
    private SysDeptCustomMapper deptCustomMapper;


    @Override
    public ResponseData selectAllOrganizationId(SysUserProvider sysUser) {
        String sid = sysUser.getSysDept().getSid();
        List<SysDeptProvider> list = deptCustomMapper.selectByParentId(sid);
        Map<String, Object> map = new HashMap<>();
        map.put("parent",sysUser.getSysDept());
        map.put("child",list);
        return new ResponseData(200,"成功",map);
    }

    /**
     * 权限管理下所有活动按月统计
     * @return
     */
    @Override
    public List<StatisticsModel> selectGroupByMonth(String organizationid) {
        List<StatisticsModel> list = statisticsMapper.selectGroupByOrganizationId(organizationid);
        return list;
    }

    /**
     * 根据组织id查询，按月统计
     * @param organizationid
     *          组织id
     * @return
     */
    @Override
    public ResponseData selectGroupByOrganizationId(String organizationid) {
        try {
            List<StatisticsModel> list = statisticsMapper.selectGroupByOrganizationId(organizationid);
            List<PartymemberVo> list1 = memberCustomMapper.selectByOrganizationId(organizationid);
            Map<String, Object> map = new HashMap<>();
            map.put("statistics", list);
            map.put("info", list1);
            return new ResponseData(200,"成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0,"失败");
        }
    }

    /**
     * 根据组织id，查询某月活动情况
     * @param date
     *      月份
     * @param organizationid
     *      组织id
     * @return
     */
    @Override
    public List<StatisticsModel> selectByMonth(Date date, String organizationid) {
        return statisticsMapper.selectByMonth(date,organizationid);
    }

    /**
     * 查询活动未到人员
     * @param id
     * 活动
     * @return
     */
    @Override
    public List<PartyMember> selectNotJoin(Integer id) {
        return statisticsMapper.selectNotJoin(id);
    }

    /**
     * 考评排名
     * @param id
     * @param date1
     * @param date2
     * @return
     */
    @Override
    public ResponseData statistics(String id, String date1, String date2) {

        try {
            List<StaticsShow> maps = new ArrayList<>();
            //活动数量排名
            List<StaticsShow> list = statisticsMapper.orderByNumber(id, date1, date2);

            //参与率排名
            List<Ratio> list1 = statisticsMapper.orderbyRatio(id, date1, date2);

            //积分排名
            List<Sum> list2 = statisticsMapper.orderBySum(id, date1, date2);
            int size = list.size();
            for (int i = 0; i < size; i++) {
                StaticsShow staticsShow = list.get(i);
                staticsShow.setRatio(list1.get(i).getRatio());
                staticsShow.setSum(list2.get(i).getSum());
                maps.add(staticsShow);
            }

            return new ResponseData(200,"成功", maps);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(0,e.getMessage());
        }
    }

    @Override
    public ResponseData selectByOrganizationId(String id) {
        List<PartymemberVo> list = new ArrayList<>();
        if (id.equals("1") || id == "1") {
            list = memberCustomMapper.selectAllByOrganizationId();
        } else {
            list = memberCustomMapper.selectByOrganizationId(id);
        }
        return new ResponseData(200, "成功", list);
    }
}
